﻿// ***********************************************************************************************
// <summary author="bill.qian" create_date="2009/9/8" modify_date="">
//         EventReceiverHelper工具类
// </summary> 
// <modify_log>
//
// </modify_log>
// <copyright file="EventReceiverHelper.cs" company="DEXTRYS" url="http://www.dextrys.com">
//           新宇软件（苏州工业园区）有限公司
// </copyright>
// ***********************************************************************************************
namespace Dextrys.SharePoint.Util
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using Microsoft.SharePoint;

    /// <summary>
    /// EventReciever工具类
    /// </summary>
    public static class EventReceiverHelper
    {
        /// <summary>
        /// 清除某个List上的全部EventReceiver
        /// </summary>
        /// <param name="list">待清除EventHandler的列表</param>
        public static void ClearEventReceiver(SPList list)
        {
            while (list.EventReceivers.Count > 0)
            {
                list.EventReceivers[0].Delete();
            }

            list.Update();
        }

        /// <summary>
        /// 注册EventHandler
        /// </summary>
        /// <param name="list">需要注册的列表</param>
        /// <param name="eventReceiverType">EventHandler类型</param>
        /// <param name="assemblyName">Assembly的全名或者dll的路径</param>
        /// <param name="className">类名</param>
        public static void RegisterEventReceiver(SPList list, SPEventReceiverType eventReceiverType, string assemblyName, string className)
        {
            assemblyName = assemblyName.Trim();
            className = className.Trim();

            if (assemblyName.ToLower().EndsWith(".dll"))
            { 
                // 如果是文件，则获取此Assembly的全名称
                assemblyName = Assembly.LoadFile(assemblyName).FullName;
            }

            bool alreadyExist = false;
            foreach (SPEventReceiverDefinition eventReceiver in list.EventReceivers)
            {
                if (eventReceiver.Type == eventReceiverType
                    && eventReceiver.Assembly == assemblyName
                    && eventReceiver.Class == className)
                {
                    alreadyExist = true;
                    break;
                }
            }

            if (!alreadyExist)
            {
                list.EventReceivers.Add(eventReceiverType, assemblyName, className);
                list.Update();
            }
        }

        /// <summary>
        /// 注册EventHandler
        /// </summary>
        /// <param name="list">带注册EventHandler的列表</param>
        /// <param name="eventReceiverType">EventHandler类型</param>
        /// <param name="type">EventHandler的.net类型</param>
        public static void RegisterEventReceiver(SPList list, SPEventReceiverType eventReceiverType, Type type)
        {
            string asmName = type.Assembly.FullName;
            string clsName = type.FullName;

            RegisterEventReceiver(list, eventReceiverType, asmName, clsName);
        }
    }
}
